﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BuckleyChurch.Business;
using BuckleyChurch.Business.Utility;
using BuckleyChurch.Business.Enumerations;

namespace ChurchData.EBulletin
{
    public partial class EBulletinSignups : ChurchForm
    {
        public EBulletinSignups()
        {
            InitializeComponent();
            RefreshGrid();
        }

        private void SetAction()
        {
            if (grdEBulletin.SelectedRows.Count == 0)
            {
                MessageBox.Show("Please select the row to update");
                return;
            }

            int tmpPK = (int)grdEBulletin.SelectedRows[0].Cells["TempSignupPK"].Value;
            List<BuckleyChurch.Business.Utility.Person> lstPersons = (List<BuckleyChurch.Business.Utility.Person>)grdEBulletin.DataSource;

            Person person = (from p in lstPersons
                             where p.TempSignupPK == tmpPK
                             select p).First();

            EBulletinSetAction frm = new EBulletinSetAction(person);
            frm.ShowDialog();

            grdEBulletin.Refresh();
        }

        private void RefreshGrid()
        {
            grdEBulletin.DataSource = null;

            List<BuckleyChurch.Business.Utility.Person> lstPersons = BuckleyChurch.Business.ElectronicBulletin.EBulletin.GetSyncRecords();
            grdEBulletin.DataSource = lstPersons;
            grdEBulletin.Columns["PeoplePK"].Visible = false;
            grdEBulletin.Columns["TempSignupPK"].Visible = false;
            grdEBulletin.Columns["SubmitDate"].Visible = false;
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            List<BuckleyChurch.Business.Utility.Person> lstPersons = (List<BuckleyChurch.Business.Utility.Person>)grdEBulletin.DataSource;
            ValidationResult result = BuckleyChurch.Business.ElectronicBulletin.EBulletin.ProcessSyncRecords(lstPersons);
            MessageBox.Show("Done");
            RefreshGrid();
        }

        private void btnSetAction_Click(object sender, EventArgs e)
        {
            SetAction();
        }

        private void grdEBulletin_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            SetAction();
        }
    }
}
